//
//  FindPwdViewController.m
//  Job
//
//  Created by 桑赐相 on 2018/8/27.
//  Copyright © 2018年 桑赐相. All rights reserved.
//

#import "FindPwdViewController.h"

@interface FindPwdViewController ()
/** 手机号 */
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (nonatomic,copy) NSString *phone;
/** 验证码 */
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
/** 新密码 */
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
/** 确认密码 */
@property (weak, nonatomic) IBOutlet UITextField *entPwdTextField;
/** 获取短信验证码 */
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;
/** 确认按钮 */
@property (weak, nonatomic) IBOutlet UIButton *enterButton;

/** 倒计时 */
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,assign) NSTimeInterval timeInterval;
@end

@implementation FindPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"找回密码";
    self.timeInterval = 60;
    [self.getCodeBtn setLayerWithRadius:5 color:ClearColor width:0.0];
    [self.enterButton setLayerWithRadius:5 color:ClearColor width:0.0];
}

- (IBAction)getCodeAction:(UIButton *)sender {
    self.phone = self.phoneTextField.text;
    if (self.phone.length <= 0) {
        [iToast showCenter:@"手机号不能为空!"];
        return;
    }
    if (![self.phone isPhoneNumber]) {
        [iToast showCenter:@"手机号格式不对"];
        return;
    }
    
    sender.enabled = NO;
    [sender setTitle:[NSString stringWithFormat:@"%gs",_timeInterval] forState:UIControlStateDisabled];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(interval) userInfo:nil repeats:YES];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"phone"] = self.phone;
    dict[@"type"] = @"findPwd";
    [HTTPTools GET:getCodeUrl parameters:dict callback:^(id result, NSString *msg, NSError *error) {
        if (!error) {
            if (!msg) {
                [iToast showCenter:@"短信发送成功！"];
                
            } else {
                [iToast showCenter:msg];
            }
        }
    }];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
}

-(void)interval{
    _timeInterval--;
    [self.getCodeBtn setTitle:[NSString stringWithFormat:@"%gs",_timeInterval] forState:UIControlStateDisabled];
    if (_timeInterval < 0) {
        _timeInterval = 60;
        self.getCodeBtn.enabled = YES;
        [self.timer invalidate];
        self.timer = nil;
    }
}


- (IBAction)enterAction:(UIButton *)sender {
    NSString *phone = self.phoneTextField.text;
    NSString *code = self.codeTextField.text;
    NSString *pwd = self.passwordTextField.text;
    NSString *enterPwd = self.entPwdTextField.text;
    if (code.length <= 0) {
        [iToast showCenter:@"请输入手机验证码"];
        return;
    }
    if (pwd.length <= 0) {
        [iToast showCenter:@"请输入密码"];
        return;
    }
    if (pwd.length < 6) {
        [iToast showCenter:@"密码不能少于6位"];
        return;
    }
    if (![pwd isEqualToString:enterPwd]) {
        [iToast showCenter:@"两次密码不一样"];
        return;
    }
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"account"] = phone;
    dict[@"password"] = pwd;
    dict[@"code"] = code;
    
    [HTTPTools POST:findPwdUrl parameters:dict callback:^(id result, NSString *msg, NSError *error) {
        if (!error) {
            if (!msg) {
                if (self.timer) {
                    [self.timer invalidate];
                    self.timer = nil;
                }
                
                [UserModel removeUserInfo];
                [MBProgressHUD showSuccess:@"密码重置成功"];
                [self.navigationController popToRootViewControllerAnimated:YES];
            }else{
                [MBProgressHUD showError:msg];
            }
        }
    }];
    
}

-(void)dealloc{
    NSLog(@"%s",__func__);
}






@end
